// This code identifies the L/R button. As they are pressed, the OLED display 
// and the LED will repond.

#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/debug.h"
#include "inc/ethernet.h"
#include "inc/flash.h"
#include "inc/gpio.h"
#include "inc/sysctl.h"
#include "inc/uart.h"
#include "utils/ustdlib.h"
#include "inc/lm3s8962.h"
#include "inc/rit128x96x4.h"

int
main(void)
{
    
    unsigned long i,j,k;
    tBoolean bswitch = false; // To label the status of LED. 

   
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, unsigned long ulLine)
    {
    }
    #endif
    
    // Set the clocking to run directly from the crystal
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_8MHZ);
    
    
    // Initialize the OLED display.
    RIT128x96x4Init(1000000);
    
    
    //Enable the peripheral for each port which is going to be used.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
 
    //
    // Enable the GPIO pin for the LED (PF0) and L/R button (PE2/3).  Set their directions.
      
    GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_2 | GPIO_PIN_3);
    GPIOPinTypeGPIOOutput ( GPIO_PORTF_BASE, GPIO_PIN_0);
    
    // Required for any input pins. Set their drive strength and pin type.
    GPIOPadConfigSet(GPIO_PORTE_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_STRENGTH_2MA,
                     GPIO_PIN_TYPE_STD_WPU);
    
   
    //
    // Loop forever.
    //
    while(1)
    {
     
      if (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2) == 0)
      {
        
        if (bswitch == false)
        {
          
         // show string "LEFT" on OLED.  
          RIT128x96x4StringDraw("LEFT", 50, 30, 15);
        
        
        //
        // Blink the LED once.
        //
        
          GPIO_PORTF_DATA_R |= 0x01;
          bswitch = true;
      
          for(i = 0; i < 250000; i++)
          {
          }

          GPIO_PORTF_DATA_R &= ~(0x01);
          bswitch = false;
          
          RIT128x96x4Clear();
        }
        
      }
      
      else if (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3) == 0)
      {
        if (bswitch == false)
        {
          
          //show string "RIGHT" on OLED display.
          RIT128x96x4StringDraw("RIGHT", 50,30,15);
        
          //Blink LED twice.
            for(j = 0; j < 2; j++)
          {
            GPIO_PORTF_DATA_R |= 0x01;
            bswitch = true;
        
            for(i = 0; i < 250000; i++)
              {
              }
  
        
       
           GPIO_PORTF_DATA_R &= ~(0x01);
           bswitch = false;
          
           for(k= 0; k < 250000; k++)
              {
              }
          
          }
          RIT128x96x4Clear();
        }
      }
    }
}